package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.article.query.ScenicReviewsQuery;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.repository.IScenicCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.List;
import java.util.Optional;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService{
    @Autowired
    private IScenicCommentRepository scenicCommentRepository;
    @Override
    public void saveOrUpdate(ScenicComment comment){
        scenicCommentRepository.save(comment);
    }

    @Override
    public int getCommentCount(Long id) {
        return scenicCommentRepository.countByScenicId(id);
    }

    @Override
    public Integer getCountByCategory(Integer category, Long id) {
        return scenicCommentRepository.countByCategoryAndScenicId(category,id);
    }

    @Override
    public void commentAdd(ScenicComment comment) {
        int rank = comment.getRank();//星星
        String tupianUrl = comment.getTupianUrl();//图片地址
        if(rank <= 2){
            comment.setCategory(1);
        }else if(rank == 3){
            comment.setCategory(2);
        }else{
            comment.setCategory(3);
        }
        //评论中的回复
        if(ScenicComment.SCENIC_COMMENT_TYPE.equals(comment.getType())){
            Optional<ScenicComment> ops = scenicCommentRepository.findById(comment.getId());
            if(ops.isPresent()){
                ScenicComment scenicComment = ops.get();
                scenicComment.getComments().add(comment);
                scenicCommentRepository.save(scenicComment);
            }
        }else {
            scenicCommentRepository.save(comment);
        }
    }

    /**
     * 获取所有评论
     *
     * @param qo
     * @param scenicId
     * @return
     */
    @Override
    public Page<ScenicComment> getComment(ScenicReviewsQuery qo, Long scenicId) {
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        return scenicCommentRepository.findByScenicId(scenicId, pageable);
    }

    @Override
    public Page<ScenicComment> getCommentByCategory(ScenicReviewsQuery qo) {
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize());
        Integer category = qo.getCategory();
        Long scenicId= qo.getScenicId();
        Page<ScenicComment> page = null;
        if (category ==0||category==4){
            page= scenicCommentRepository.findByScenicId(scenicId,pageable);
        }else {
            page= scenicCommentRepository.findByCategoryAndScenicId(category,scenicId ,pageable);
        }
        return page;
    }

    /**
     * 点赞功能
     * @param id
     * @param userId
     * @return
     */
    @Override
    public Integer strategyThumbup(String id, Long userId) {
        ScenicComment comment = scenicCommentRepository.findById(id).get();
        //获取点赞集合
        List<Long> thumpup = comment.getThumbup();
        int state = 0;//默认为点赞
        if (thumpup.contains(userId)){
            //包含就取消点选
            thumpup.remove(userId);
            //点赞数减1
            comment.setThumbupnum(comment.getThumbupnum()-1);
        }else {
            //不包含就点赞
            thumpup.add(userId);
            //点赞数加1
            comment.setThumbupnum(comment.getThumbupnum()+1);
            //将标记设置为1
            state=1;
        }
        //将数据同步到db
        this.commentAdd(comment);
        return state;
    }
}
